package spring.board;

// paramMap 는 전 페이지에서 form 값이 submit 되면서 넘어 온값이 저장
// model 은 처리가 끝나고 view 에 결과 값을 보낼때 쓰는 객체 
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import spring.board.service.LookdService;
import spring.board.vo.LoginVo;
import spring.board.vo.LookdVo;
import spring.board.vo.PicVo;

@Controller
public class LookdController {
	
	@Autowired
	private LookdService lookdService;
	
	@RequestMapping("/ProfileJoinForm.do")
	public void ProfileJoinForm(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable{
		
	}
	/*
	@RequestMapping("/login.do")
	public void login2(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		System.out.println("ddddddd");
	}*/
	
	@RequestMapping(value="login22.do", method=RequestMethod.POST)
	public String login3(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		//String id = request.getParameter("id");
		//String pw = request.getParameter("pw");
		String id = (String) paramMap.get("id");
		String pw = (String) paramMap.get("pw");
		
		boolean isLogin = false;
		
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		System.out.println(id);
		System.out.println(pw);
		
		String check = lookdService.getPw(id);
		System.out.println(check);
		
		if(check.equals(pw)) {
			isLogin = true;
			
		} else {
			isLogin = false;
		}
		System.out.println(isLogin);
		
		//ModelAndView mv = new ModelAndView();
		//mv.addObject("insertUser", "success");
		//mv.setViewName("login.do");
		
		return "login.do";
	}
	
	/*@RequestMapping(value="login2.do", method=RequestMethod.POST)
	public ModelAndView login3(HttpServletRequest request) {
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		boolean isLogin = false;
		
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		System.out.println(id);
		System.out.println(pw);
		
		String check = lookdService.getPw(id);
		System.out.println(check);
		
		if(check.equals(pw)) {
			isLogin = true;
			
		} else {
			isLogin = false;
		}
		System.out.println(isLogin);
		
		ModelAndView mv = new ModelAndView();
		//mv.addObject("insertUser", "success");
		mv.setViewName("/login.do");
		
		return mv;
	}*/
	
	
	/*@RequestMapping(value="login.do", method=RequestMethod.GET)
	public void login(HttpServletRequest request) {
		String id = request.getParameter("id");
		String pw = request.getParameter("pw");
		
		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
		System.out.println(id);
		System.out.println(pw);
		
		String check = lookdService.getPw(id);
	}*/
	
	
	
	/*
	@RequestMapping("/today2.do")
	public void today2(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("today2", lookdService.getToday2());
	}
	*/
	
	@RequestMapping(value="today2.do", method=RequestMethod.GET)
	public ModelAndView today2(/*HttpServletRequest request*/) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("today2");
		mv.addObject("today2", lookdService.getToday2());
		return mv;
	}
	
	/*@RequestMapping("/logintTest.do")
	public void Login(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		
	}*/
	
	/* 로그인 */
	@RequestMapping("/lookdomLogin.do")
	public void lookdomLogin(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		
	}
	
	/* 로그인 성공 또는 실패*/
	@RequestMapping(value = "success.do", method = RequestMethod.GET)
	public void login(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		
		boolean isLogin = false;
		
		String id = (String) paramMap.get("id");
		String pw = (String) paramMap.get("pw");
		
		//String check = lookdService.logn(id);
		String check = lookdService.getPw(id);
		
		/*System.out.println("####################################");
		System.out.println(id);
		System.out.println(check);
		System.out.println(pw);*/
		

		ModelAndView mav = new ModelAndView();

		mav.setViewName("redirect:/hello.do?isLogin="+isLogin);
		
		if(pw.equals(check)) {
			/*isLogin = true;
			mav.setViewName("redirect:/hello.do?isLogin="+isLogin);*/
			model.put("results", "성공");
		} else {
			isLogin = false;
			/*mav.setViewName("redirect:/loginFail.do?isLogin=");*/
			model.put("results", "실패");
		}
		
		//return isLogin;
		//return mav;
	}
	
	/* 사진 정보 가져오기 */
	@RequestMapping("/pic.do")
	public void pic(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		//model.put("results", lookdService.getUserList(paramMap));
		System.out.println(lookdService.getActItem());
		PicVo picVoList = lookdService.getActItem();
		model.put("results", picVoList);
	}
	
	/* JSON을 이용 */
	@RequestMapping("/listJson.do")
	public @ResponseBody Map<?, ?> listJson(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		//model.put("results", "성공");	//lookdService.getUserList(paramMap));
		LoginVo login = new LoginVo();
		login.setId("id");
		login.setPw("pw");
		
		model.put("results", login);
		
		return model;
	}
	
	/* AJAX를 이용*/
	@RequestMapping("/ajaxView.do")
	public void ajaxView(@RequestParam Map<String, Object> paramMap, ModelMap model)throws Throwable {
		
	}
	
/*	
	@RequestMapping("/login.do")
	public ModelAndView loginTest(@ModelAttribute("loginVo") LoginVo loginVo, ModelMap model) throws Throwable {
		System.out.println("id: " + loginVo.getId());
		System.out.println("id: " + loginVo.getPw());
		
		int cnt = lookdService.loginTest(loginVo);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/userList.do");
		
		return mav;
	}
	*/
	
	/*@RequestMapping("/today2.do")
	public void today2(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("today2", lookdService.getToday2());
	}*/
	
	@RequestMapping("/write.do")
	public void write(@RequestParam Map<String, Object> paramMap, ModelMap model) {
		
	}
	
	@RequestMapping("/insertLookd.do")
	public ModelAndView insertLookd(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		
		System.out.print("id: " + paramMap.get("id") + '\t');
		System.out.print("name: " + paramMap.get("name") + '\t');
		System.out.println("telnumber: " + paramMap.get("telnumber") + '\t');
		
		int cnt = lookdService.insertLookd(paramMap);
		 
		System.out.println(cnt + "건 입력되었습니다.");
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/write.do");
		
		return mav; // view에 전달
	}
	
	
	@RequestMapping("/userList.do")
	public void userList(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("results", lookdService.getUserList(paramMap));
	}
	
	@RequestMapping("/userVo.do")
	public void getVo(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		// Map<String, Object> result = new HashMap<String, Object>();
		List<LookdVo> users = lookdService.getVo();
		
		System.out.println("***************************************************");
		
		System.out.println(users);
		System.out.println(users.get(0));
		System.out.println(users.get(0).getId());
		
		model.put("user1", users.get(0));
		model.put("user2", users.get(0).getId());
	}
	/*
	@RequestMapping("/lookd_uers.do")
	public void getUsers(@RequestParam Map<String, Object> paramMap, ModelMap model) throws Throwable {
		model.put("users", lookdService.getUsers());
	}
	*/
}
